import cors from 'koa2-cors';
import compress from 'koa-compress';
import bodyParser from 'koa-bodyparser';
import resourcesProxy from './proxy/resourcesProxy';
import errHandler from './lib/errHandler';
import appFileRoutes from '../routes/AppFileRoutes';
import ProxyRoutes from '../routes/ProxyRoutes';
import DBRoutes from '../routes/DBRoutes';
import AuthorizeRoutes from '../routes/AuthorizeRoutes';
import configTool from '../com/configTool';
import logHandler from './lib/logHandler';
import IPAuthorityHandler from './lib/IPAuthorityHandler';
import mapServices from "../routes/MapServicesRoutes";

enum RouteType {
    client = 'client',
    manage = 'manage'
}

function clientMiddleWare(app: any) {
    // 允许跨域请求
    configTool.config.clientCORS && app.use(cors());
    app.use(bodyParser());

    app.use(errHandler());
    app.use(logHandler(RouteType.client));
    app.use(IPAuthorityHandler(['*'], configTool.config.clientBlackList));


    app.use(compress({br: false}));

    resourcesProxy(app, configTool.wwwPath);


    // 数据转发缓存服务
    app.use(ProxyRoutes.routes());
    app.use(mapServices.routes())

    app.use(appFileRoutes.routes());
    app.use(DBRoutes.routes());
    app.use(AuthorizeRoutes.routes());
}

export default clientMiddleWare;
